package cn.imu.appdevexperiment2020.Application.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import cn.imu.appdevexperiment2020.Application.api.ApiClient;
import cn.imu.appdevexperiment2020.Application.bean.user.EditReceiveBean;
import cn.imu.appdevexperiment2020.Application.bean.user.EditSendBean;
import cn.imu.appdevexperiment2020.Application.util.AuthUtil;
import cn.imu.appdevexperiment2020.R;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EditActivity extends AppCompatActivity {
    public static final String TAG="Edit";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        Button editButton = findViewById(R.id.edit);
        EditText userNameEt =findViewById(R.id.edit_userName);
        EditText updataByEt =findViewById(R.id.edit_updataBy);
        EditText nicknameEt =findViewById(R.id.edit_nickName);
        EditText classIdEt =findViewById(R.id.edit_classId);
        EditText emailEt =findViewById(R.id.edit_email);
        EditText phoneNumberEt =findViewById(R.id.edit_phoneNumber);
        EditText userIdEt =findViewById(R.id.edit_userId);
        editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RadioGroup sexGroup=findViewById(R.id.edit_gender);
                RadioGroup statusGroup=findViewById(R.id.edit_status);
                RadioButton sexButton=findViewById(sexGroup.getCheckedRadioButtonId());
                RadioButton statusButton=findViewById(statusGroup.getCheckedRadioButtonId());
                String username= userNameEt.getText().toString();
                String updateBy= updataByEt.getText().toString();
                String nickname= nicknameEt.getText().toString();
                String classId= classIdEt.getText().toString();
                String email= emailEt.getText().toString();
                String phoneNumber= phoneNumberEt.getText().toString();
                String userId=userIdEt.getText().toString();
                String sex = sexButton.getTag().toString();
                String status = statusButton.getTag().toString();
                EditSendBean editSendBean = new EditSendBean();
                editSendBean.setUserName(username);
                editSendBean.setUpdateBy(updateBy);
                editSendBean.setNickName(nickname);
                editSendBean.setClassesId(classId);
                editSendBean.setEmail(email);
                editSendBean.setPhonenumber(phoneNumber);
                editSendBean.setUserId(userId);
                editSendBean.setSex(sex);
                editSendBean.setStatus(status);
                Call<EditReceiveBean> call= ApiClient.getInstance(getApplicationContext()).getReturnData(AuthUtil.GetAuth(),editSendBean);
                call.enqueue(new Callback<EditReceiveBean>() {
                    @Override
                    public void onResponse(Call<EditReceiveBean> call, Response<EditReceiveBean> response) {
                        if(null!=response.body())
                        {
                            if(200==response.body().getCode())
                            {
                                Log.d(TAG, response.body().getMsg());
                                Toast.makeText(getApplicationContext(),"编辑成功",Toast.LENGTH_SHORT).show();
                                finish();
                            }else{
                                Toast.makeText(getApplicationContext(),response.body().getMsg(),Toast.LENGTH_SHORT).show();
                            }
                        }else
                            Toast.makeText(getApplicationContext(),"网络连接失败",Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onFailure(Call<EditReceiveBean> call, Throwable t) {
                        Toast.makeText(getApplicationContext(),"网络连接失败",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}